<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>输入提示后查询</title>
    <link rel="stylesheet" href="https://cache.amap.com/lbs/static/main.css"/>
    <script type="text/javascript"
            src="https://webapi.amap.com/maps?v=1.4.15&key=18426d6cc089b14ed6d6ef4a517d4430"></script>
    <style type="text/css">
        body {
            font-size: 12px;
        }
        #tip {
            background-color: #ddf;
            color: #333;
            border: 1px solid silver;
            box-shadow: 3px 4px 3px 0px silver;
            position: absolute;
            top: 10px;
            right: 10px;
            border-radius: 5px;
            overflow: hidden;
            line-height: 20px;
        }
        #tip input[type="text"] {
            height: 25px;
            border: 0;
            padding-left: 5px;
            width: 280px;
            border-radius: 3px;
            outline: none;
        }

    </style>
</head>
<body>
<div id="mapContainer"></div>
<div id="tip">
    <input type="text" id="keyword" name="keyword" value="请输入关键字：(选定后搜索)" onfocus='this.value=""'/>
</div>
<script type="text/javascript">
    // var windowsArr = [];
    // var marker = [];
    var map = new AMap.Map("mapContainer", {
        resizeEnable: true,
        center: [116.397428, 39.90923],//地图中心点
        zoom: 13,//地图显示的缩放级别
        keyboardEnable: false
    });
    //自动适配到合适的视野范围
    map.setFitView();
    AMap.plugin(['AMap.Autocomplete','AMap.PlaceSearch'],function(){
        var autoOptions = {
            city: "全国", //城市，默认全国
            input: "keyword"//使用联想输入的input的id
        };
        autocomplete= new AMap.Autocomplete(autoOptions);

        var placeSearch = new AMap.PlaceSearch({
            city: '全国'
            // map: map
        });
        autocomplete.on('select',function (e) {
            console.log(e);
            console.log(e.poi.location.lng);
            console.log(e.poi.location.lat);
            var lng = e.poi.location.lng;
            var lat = e.poi.location.lat;
            //TODO 针对选中的poi实现自己的功能
            placeSearch.setCity(e.poi.adcode);
            placeSearch.search(e.poi.name);
            var content = [
                `<div>
                        <img src="http://webapi.amap.com/images/autonavi.png">
                        <div style="padding: 0px 0px 4px">
                            坐标 : `+lng+`,`+lat+`<br>
                        </div>
                    </div>`];
            addMarker(lng, lat);
            infowindowOpen(lng,lat,content);
        })
    });
    //地图点击事件
    map.on('click', function (ev) {
        // 触发事件的地理坐标，AMap.LngLat 类型
        var lnglat = ev.lnglat;
        var content = [
            `<div>
                        <img src="http://webapi.amap.com/images/autonavi.png">
                        <div style="padding: 0px 0px 4px">
                            坐标 : `+lnglat+`<br>
                        </div>
                    </div>`];
        // console.log(lnglat.getLng)
        addMarker(lnglat.getLng(),lnglat.getLat());
        infowindowOpen(lnglat.getLng(),lnglat.getLat(),content);

    });

    //添加marker标记
    function addMarker(lng,lat) {
        map.clearMap();
        marker = new AMap.Marker({
            map: map,
            position: [lng,lat]
        });
    }
    //添加右键菜单
    let infoWindow = null;

    function infowindowOpen(lng,lat, content) {
        //创建inforwindow实例
        infoWindow = new AMap.InfoWindow({
            anchor: 'top-left',
            content: content.join("") //直接加可以,间接加,如现在的方式,需要转为字符串
        });
        infoWindow.open(map, [lng,lat]);
    }
</script>
<script type="text/javascript" src="https://webapi.amap.com/demos/js/liteToolbar.js"></script>
</body>
</html>